package com.app.controller;

import com.app.model.JobPublishModel;
import com.app.service.JobPublishService;
import com.app.service.MsgVerifyService;
import com.app.util.PhoneNumberUtil;
import com.app.util.Result;

import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@Controller
@RequestMapping("/identifiy")
public class MsgIdentifiyController {

    @Autowired
    MsgVerifyService msgVerifyService;
    @Autowired
    JobPublishService jobPublishService;

    /**
     * 发送短信
     */
    @Synchronized
    @GetMapping("/saveMsgIdentifiy")
    @ResponseBody
    public Result<Object> saveMsgIdentifiy(String mobile, Integer type) throws IOException {

        if(PhoneNumberUtil.isValidPhoneNumber(mobile))
        {
            int code = msgVerifyService.saveMsgIdentifiy(mobile, type);
            if (code == 200) {
                return Result.success("短信已发送，请1分钟后再试");
            } else {
                return Result.error(code, "操作失败");
            }
        } else {
            return Result.error(500, "手机格式错误");
        }

    }


    @GetMapping("/smsReminder")
    @ResponseBody
    public Result<Object> smsReminder(String mobile,Integer type,Integer pId) {

        JobPublishModel model = jobPublishService.findJobByPost(pId);

        if(type==3&&model==null)
        {
            return Result.success("未匹配到岗位，不发送");
        }
        int code = msgVerifyService.smsReminder(mobile,type);
        if (code == 200) {
            return Result.success("短信已发送，请1分钟后再试");
        } else {
            return Result.error(code, "操作失败");
        }
    }


}
